package com.abredatos.json;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.security.MessageDigest;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONObject;


public class JSONPetition {

	private final static String sUserAgent = "AbreDatos2011-Android";
	
	private Header       header;
	private String       uri = "http://ssweb.mpt.es/infofondos/map/gridResults?sEcho=5&iColumns=6&sColumns=&iDisplayStart=0&iDisplayLength=428&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&sSearch_5=&bRegex_5=false&bSearchable_5=true&iSortingCols=1&iSortCol_0=0&sSortDir_0=asc&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=true&bSortable_5=true&fondo=&provincia=28&municipio=3535";
	private JSONReader 	 jsonReader;
	
	private JSONObject   json;
	private HttpResponse response;
	private HttpPost     post;
	
	
	public JSONPetition() {
		uri = "";
		setJson(new JSONObject());
		post = new HttpPost(uri);
	}
	
	public JSONPetition(String url) {
		uri = url;
		setJson(new JSONObject());
		post = new HttpPost(uri);
	}
	
	public void setHeader(String name, String value) {
		post.setHeader(name, value);
	}
	
	public void launchConnection() {
		HttpClient client = new DefaultHttpClient();

		try{
        	HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
        	 
        	post.setHeader("User-Agent", sUserAgent);
           post.setHeader("Content-type", "application/json");
             
            //Necessary parameters to make the JSON work
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            post.getParams().setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
            post.getParams().setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
            
            //Creating JSON, and sending to the server
            JSONEntity jsonEntity = new JSONEntity(json);
            post.setEntity(jsonEntity);
            response = client.execute(post); 
            System.out.println("");
        } catch(Exception e){
            e.printStackTrace();
            //createDialog("Error", "Cannot establish Connection");
        }
	}
	
	public void setJson(JSONObject json) {
		this.json = json;
	}

	public JSONObject getJson() {
		return json;
	}

	public void setResponse(HttpResponse response) {
		this.response = response;
	}

	public HttpResponse getResponse() {
		return response;
	}